$(function () {
    obj.init();
});
var obj =new function () {
    'use strict';
    return {
        bookId:null,
        form: null,
        init:function () {
            this.bookId  = xhTool.getQuery('bookId');
            this.form = new xhForm($('#apply_Plan_form'));
            this.listener();
        },
        listener:function () {
            var  $this = this;
            var xh_form = $('#apply_Plan_form');
            //通过
            xh_form.on('click','[id^="checkTrue_"]',function () {
                var id = $(this).attr("id").split("checkTrue_")[1];
                $this.funObj().checkTrue(id);
            });
            //不通过
            xh_form.on('click','[id^="checkFalse_"]',function () {
                var id = $(this).attr("id").split("checkFalse_")[1];
                $this.funObj().checkFalse(id);
            });
            //取消
            xh_form.on('click','[id^="checkDel_"]',function () {
                var id = $(this).attr("id").split("checkDel_")[1];
                $this.funObj().checkDel(id);
            });
            //查看不通过原因
            xh_form.on('click','.reason',function () {
                var reason = $(this).attr('reasonMsg');
                window.top.xhTop.showMessage({
                    title: '[原因详情]',
                    text: reason
                });
            });
            //查看取消原因
            xh_form.on('click','.cancel',function () {
                var cancel = $(this).attr('cancelMsg');
                window.top.xhTop.showMessage({
                    title: '[原因详情]',
                    text: cancel
                });
            });
        },
        funObj:function () {
            var $this = this;
            var xh_form = $('#apply_Plan_form');
            return {
                checkTrue: function (id) {
                    xhExtra.askChangeStatus('res/checkTrueApplyPlan', {id:id}, null, '确认同意？', function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        location.reload();
                    });
                },
                checkFalse: function (id) {
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '填写原因',
                        html: '<div><textarea type="text" id="reason" class="swal2-textarea" placeholder="必填"></textarea></div>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var reason = $('#reason', window.top.document).val();
                                if(!reason){
                                    reject('请填写原因');
                                } else {
                                    xhExtra.ajaxRequest('res/checkFalseApplyPlan', {
                                        id: id,
                                        reason: reason
                                    }, {success: true});
                                    resolve();
                                    location.reload();
                                }
                            })
                        }
                    });

                },
                checkDel: function (id) {
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '填写原因',
                        html: '<div><textarea type="text" id="reason" class="swal2-textarea" placeholder="必填"></textarea></div>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var reason = $('#reason', window.top.document).val();
                                if(!reason){
                                    reject('请填写原因');
                                } else {
                                    xhExtra.ajaxRequest('res/delApplyPlan', {
                                        id: id,
                                        reason: reason
                                    }, {success: true});
                                    resolve();
                                    location.reload();
                                }
                            })
                        }
                    });
                }
            }
        }
    }
};
